package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.MallActivityDao;
import com.sc.nft.dao.MallPreferenceActivityDao;
import com.sc.nft.entity.MallActivity;
import com.sc.nft.entity.MallPreferenceActivity;
import com.sc.nft.entity.dto.MallPreferenceActivityAdminDTO;
import com.sc.nft.entity.vo.MallPreferenceActivityAdminVO;
import com.sc.nft.entity.vo.MallPreferenceActivityVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallPreferenceActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 好物优选 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallPreferenceActivityServiceImpl  implements MallPreferenceActivityService {

    private final MallPreferenceActivityDao mallPreferenceActivityDao;
    private final MallActivityDao activityDao;


    /**
     * 添加好物优选
     *
     * @param mallPreferenceActivityVO
     */
    @Override
    public void add(MallPreferenceActivityVO mallPreferenceActivityVO) {
        for (Long activityId : mallPreferenceActivityVO.getActivityIds()) {
            MallActivity mallActivity = activityDao.getById(activityId);
            if (Objects.nonNull(mallActivity) && mallActivity.getStatus() && Objects.isNull(mallPreferenceActivityDao.getByActivityId(activityId))) {
                MallPreferenceActivity mallPreferenceActivity = new MallPreferenceActivity();
                mallPreferenceActivity.setDaoId(mallActivity.getDaoId());
                mallPreferenceActivity.setActivityId(activityId);
                mallPreferenceActivity.setSort(0);
                mallPreferenceActivity.setStatus(Boolean.FALSE);
                mallPreferenceActivity.insert();

            }
        }
    }

    /**
     * 移除好物优选
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        MallPreferenceActivity mallPreferenceActivity = mallPreferenceActivityDao.getById(id);
        Assert.isFalse(mallPreferenceActivity.getStatus(),()->new GlobalRunTimeException("好物优选已上架,不能删除"));
        Assert.notNull(mallPreferenceActivity,()->new GlobalRunTimeException("好物优选商品不存在"));
        mallPreferenceActivity.deleteById();
    }

    /**
     * 修改好物优选上架状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Boolean status) {
        if (status){
            Integer theNumberOfListings = mallPreferenceActivityDao.getTheNumberOfListings();
            if (theNumberOfListings >= 20){
                throw new GlobalRunTimeException("上架数量已达上限");
            }
        }
        mallPreferenceActivityDao.updateStatus(id, status);
    }

    /**
     * 修改好物优选排序
     *
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        mallPreferenceActivityDao.updateSort(id, sort);
    }

    /**
     * 获取好物优选列表
     *
     * @param mallPreferenceActivityAdminVO
     * @return
     */
    @Override
    public Page<MallPreferenceActivityAdminDTO> pageAdmin(MallPreferenceActivityAdminVO mallPreferenceActivityAdminVO) {
        Page<MallPreferenceActivityAdminDTO> page = mallPreferenceActivityDao.pageAdmin(mallPreferenceActivityAdminVO.getProductName(), mallPreferenceActivityAdminVO.getDaoId(), mallPreferenceActivityAdminVO.getProductType(), mallPreferenceActivityAdminVO.getActivityType(), mallPreferenceActivityAdminVO.getCategoryId(), mallPreferenceActivityAdminVO.getStatus(), mallPreferenceActivityAdminVO.getPageNo(), mallPreferenceActivityAdminVO.getPageSize());
        for (MallPreferenceActivityAdminDTO record : page.getRecords()) {
            if (StrUtil.isNotBlank(record.getCoverImage())) {
                record.setCoverImage(StrUtil.split(record.getCoverImage(), ",").get(0));
            }
        }
        return page;
    }

    @Override
    public MallPreferenceActivity getByActivityId(Long activityId) {
        return mallPreferenceActivityDao.getByActivityId(activityId);
    }

    @Override
    public MallPreferenceActivity getById(Long id) {
        return mallPreferenceActivityDao.getById(id);
    }

    @Override
    public void test() {

    }
}
